﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using osu_BeatmapAutoLoader.Properties;

namespace osu_BeatmapAutoLoader
{
    internal static class Program
    {
        public static MainForm Mainform;
        public static string Path;
        public static string OsuPatch;

        /// <summary>
        ///     应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Path = Application.StartupPath + "\\";
            OsuPatch = FindPatch();
            if (OsuPatch != null)
            {
                Process.Start(OsuPatch);
            }
#if !DEBUG
            CreateFile(Path);
#endif
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Mainform = new MainForm();
#if !DEBUG
            Application.ApplicationExit += (sender, args) => DeleteFile(Path);
#endif
            Application.Run(Mainform);
        }

        private static string FindPatch()
        {
            if (File.Exists(Path + "osu!.exe"))
            {
                Process.Start("osu!.exe");
                return Path + "osu!.exe";
            }
            char[] alphabet = "CDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            string x86 = ":\\Program Files (x86)\\osu!\\";
            string x64 = ":\\Program Files\\osu!\\";
            foreach (char disk in alphabet)
            {
                if (File.Exists(disk + x86 + "osu!.exe"))
                    return disk + x86 + "osu!.exe";
                if (File.Exists(disk + x64 + "osu!.exe"))
                    return disk + x64 + "osu!.exe";
            }
            return null;
        }


        private static void CreateFile(string path)
        {
            if (!File.Exists(path + "EasyHook.dll")) //文件不存在
            {
                var fs = new FileStream(path + "EasyHook.dll", FileMode.CreateNew, FileAccess.Write);
                byte[] buffer = Resources.EasyHook;
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
            if (!File.Exists(path + "EasyHook32.dll")) //文件不存在
            {
                var fs = new FileStream(path + "EasyHook32.dll", FileMode.CreateNew, FileAccess.Write);
                byte[] buffer = Resources.EasyHook32;
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
            if (!File.Exists(path + "HtmlAgilityPack.dll")) //文件不存在
            {
                var fs = new FileStream(path + "HtmlAgilityPack.dll", FileMode.CreateNew, FileAccess.Write);
                byte[] buffer = Resources.HtmlAgilityPack;
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
            if (!File.Exists(path + "Injector.dll")) //文件不存在
            {
                var fs = new FileStream(path + "Injector.dll", FileMode.CreateNew, FileAccess.Write);
                byte[] buffer = Resources.Injector;
                fs.Write(buffer, 0, buffer.Length);
                fs.Close();
            }
        }

        private static void DeleteFile(string path)
        {
            try
            {
                if (File.Exists(path + "EasyHook.dll")) //文件存在
                {
                    File.Delete(path + "EasyHook.dll");
                }
                if (File.Exists(path + "EasyHook32.dll")) //文件存在
                {
                    File.Delete(path + "EasyHook32.dll");
                }
                if (File.Exists(path + "HtmlAgilityPack.dll")) //文件存在
                {
                    File.Delete(path + "HtmlAgilityPack.dll");
                }
                if (File.Exists(path + "Injector.dll")) //文件不存在
                {
                    File.Delete(path + "Injector.dll");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    } 
}
       